package customer

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/common/response"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type PageViewApi struct{}

// RecordPageView 记录页面访问
// @Tags PageView
// @Summary 记录页面访问
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "记录成功"
// @Router /pageView/record [post]
func (b *PageViewApi) RecordPageView(c *gin.Context) {
	ip := c.ClientIP()
	path := c.Request.URL.Path
	userAgent := c.Request.UserAgent()

	err := pageViewService.RecordPageView(ip, path, userAgent)
	if err != nil {
		global.GVA_LOG.Error("记录页面访问失败!", zap.Error(err))
		response.FailWithMessage("记录页面访问失败", c)
		return
	}

	response.OkWithMessage("记录成功", c)
}

// GetPageViewStats 获取页面访问统计
// @Tags PageView
// @Summary 获取页面访问统计
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=int64,msg=string} "获取成功"
// @Router /pageView/stats [get]
func (b *PageViewApi) GetPageViewStats(c *gin.Context) {
	// 简化返回，只返回总访问量
	total, err := pageViewService.GetTotalPageViews()
	if err != nil {
		global.GVA_LOG.Error("获取页面访问统计失败!", zap.Error(err))
		response.FailWithMessage("获取页面访问统计失败", c)
		return
	}

	response.OkWithData(gin.H{"total": total}, c)
}
